package com.xwaf.platform.web.controller.auth;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.constants.BaseConstants;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.entity.auth.Account;
import com.xwaf.platform.system.api.service.auth.AccountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * <p>
 * 用户账户表控制器
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2020-1-24 17:51:29 <br>
 * @see 无<br>
 *      Copyright (c) 2020, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@RestController
@Api(tags = "用户账户表")
@RequestMapping(value = "/m")
public class AccountController extends BaseController {

	@Autowired
	private AccountService accountService;

	@ApiOperation("分页查询")
	@PostMapping(value = "/account/page", produces = "application/json;charset=UTF-8")
	public CommonResponse page(@RequestParam("currentPage") String currentPage,
			@RequestParam("pageSize") String pageSize, @RequestBody Account account) {
		CommonResponse resp = CommonResponse.createCommonResponse();
		if (!StringUtils.isNotBlank(currentPage) & !StringUtils.isNotBlank(pageSize)) {
			currentPage = "1";
			pageSize = "10";
		}
		Page<Account> page = new Page<Account>(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
		Page<Account> result = accountService.selPageList(page, account);
		resp.setData(result);
		return resp;
	}

	@ApiOperation(value = "根据条件查询用户账户表", notes = "根据条件查询用户账户表信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "/accounts", produces = "application/json;charset=UTF-8")
	public CommonResponse list(@RequestParam("name") String name) {
		Wrapper<Account> wrapper = new EntityWrapper<Account>();
		wrapper.like("name", name);
		List<Account> list = accountService.selectList(wrapper);
		return CommonResponse.createCommonResponse(list);
	}

	@ApiOperation(value = "新增用户账户表", notes = "新增用户账户表信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping(value = "/account", produces = "application/json;charset=UTF-8")
	public CommonResponse addAccount(@RequestBody Account account) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			account.setCreateBy(loginUser.getId());
			account.setUpdateBy(loginUser.getId());
		}
		account.setId(IdWorker.get32UUID());
		boolean isAdded = accountService.insert(account);
		if (isAdded) {
			return CommonResponse.createCommonResponse(account);
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "新增失败!");
		}
	}

	@ApiOperation(value = "修改用户账户表", notes = "修改用户账户表信息", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	@PutMapping(value = "/account/{accountId}", produces = "application/json;charset=UTF-8")
	public CommonResponse updateAccount(@PathVariable("accountId") String accountId, @RequestBody Account account) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			account.setUpdateBy(loginUser.getId());
		}
		account.setId(accountId);
		boolean isUpdated = accountService.updateById(account);
		if (isUpdated) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "修改失败!");
		}
	}

	@ApiOperation(value = "删除用户账户表", notes = "删除用户账户表信息", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@DeleteMapping(value = "/account/{accountIds}", produces = "application/json;charset=UTF-8")
	public CommonResponse delById(@PathVariable("accountIds") List<String> accountIds) {
		// boolean isDel = accountService.deleteBatchIds(accountIds);
		boolean isDel = true;
		if (isDel) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "删除失败!");
		}
	}

	@ApiOperation(value = "查看用户账户表详情", notes = "查看用户账户表详情信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "/account/{accountId}", produces = "application/json;charset=UTF-8")
	public CommonResponse getInfoById(@PathVariable("accountId") String accountId) {
		Account account = accountService.selectById(accountId);
		return CommonResponse.createCommonResponse(account);
	}
}
